/************************************************************************************/
/*  Program name CAPFAIMP_ADJUSTMENTS.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 17 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_ADJUSTMENTS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_ADJUSTMENTS;
	
	lv_COA_ID		NUMBER;

BEGIN
	UPDATE
		FA_ADJUSTMENTS
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_ADJUSTMENTS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_COA_ID := CAPBRIMP_COMMON.GetCOAIDfromBTC(C1.Book_Type_Code);
			
			INSERT INTO FA_ADJUSTMENTS
			VALUES
			(
			CAPBRIMP_MAPPING.GetTXNHeaderIDMapping(C1.TRANSACTION_HEADER_ID),
			C1.SOURCE_TYPE_CODE,
			C1.ADJUSTMENT_TYPE,
			C1.DEBIT_CREDIT_FLAG,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CODE_COMBINATION_ID_D),
			C1.BOOK_TYPE_CODE,
			CAPBRIMP_MAPPING.GetAssetIDMapping(C1.ASSET_ID),
			C1.ADJUSTMENT_AMOUNT,
			CAPBRIMP_MAPPING.GetFADistIDMapping(C1.DISTRIBUTION_ID),
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.ANNUALIZED_ADJUSTMENT,
			CAPBRIMP_MAPPING.GetJEHeaderIDMapping(C1.JE_HEADER_ID),
			C1.JE_LINE_NUM,
			C1.PERIOD_COUNTER_ADJUSTED,
			C1.PERIOD_COUNTER_CREATED,
			C1.ASSET_INVOICE_ID,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.DEPRN_OVERRIDE_FLAG
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/